package com.dq.controller;

import com.dq.mapper.GoodsMapper;
import com.dq.mapper.UserMapper;
import com.dq.pojo.Goods;
import com.dq.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class AjaxController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @RequestMapping("/a1")
    @ResponseBody
    public String ajax1(@RequestParam(value = "id",required = false) String id, @RequestParam(value = "password",required = false) String password) throws IOException {
        String msg = "null";

        if (id!=null){
            if (userMapper.getUserById(id)!=null){
                msg="OK";

            }else {
               msg="用户名输入错误";
            }
        }
        if (password!=null){

            if ("123456".equals(password)){
                msg="OK";
            }else {
                msg="密码输入有误";
            }
        }
        return msg;//由于@RestController注解，将msg转成json格式返回
    }

    @RequestMapping("/a2")
    @ResponseBody
    public String registerInfo(@RequestParam(value = "id",required = false)String id){
        String msg = "";
        //模拟数据库中存在数据
        if (id!=null){
            if (userMapper.getUserById(id)!=null){
                msg="该用户名已注册，不可用";

            }else {
                msg="该用户名可用";
            }
        }
        return msg;
    }

    @RequestMapping("/a3/{id}")
    @ResponseBody
    public String ajax3(@PathVariable("id")String id) throws IOException {
        String msg = "null";

        if (id!=null){
            if (userMapper.getUserById(id)!=null){
                msg="OK";

            }else {
                msg="用户名输入错误";
            }
        }

        return msg;//由于@RestController注解，将msg转成json格式返回
    }

    @RequestMapping("/keySearch")
    @ResponseBody
    public List<Goods> keySearch(String keyword){
        List<Goods> keyGoods = goodsMapper.getKeyGoods(keyword);
        return keyGoods;
    }

    @RequestMapping("/WB")
    public List<Goods> wb(@RequestParam(value = "page",defaultValue = "1")Integer page){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String id = userDetails.getUsername();
        PageHelper.startPage(page,1);
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsMapper.getGoodsByDelegateId(id));
        List<Goods> goods = pageInfo.getList();

        return goods;
    }

}
